<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="javax.jdo.Query" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.cse210.grocerwe.common.*" %>

<html>
  <body>
		<%
		    UserService userService = UserServiceFactory.getUserService();
		    User user = userService.getCurrentUser();
		    if (user != null) {
		%>
				<p>Hello, <%= user.getNickname() %>! (You can
				<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
				<p>
				<a href="/start">home / check requests </a> &nbsp;
				<a href="/db">view item database</a>&nbsp;
				<a href="/groups">view groups</a></p>
				
				<%
				    PersistenceManager pm = PMF.get().getPersistenceManager();
				    Query q = pm.newQuery(GroupRequest.class);
				    q.setFilter("user == x || user == y");
				    q.declareParameters("String x, String y"); 
				    List<GroupRequest> requests = (List<GroupRequest>) q.execute(user.getNickname().toLowerCase(),user.getEmail().toLowerCase());
				    if (!(requests.isEmpty()))
				    {
				%>
		    			<table cellpadding="2" cellspacing="2" width="100%">
						<tr>
						<td bgcolor="#cccccc">invited by</td>
						<td bgcolor="#cccccc">group name</td>
						<td bgcolor="#cccccc">Accept?</td>
						<td bgcolor="#cccccc">Reject?</td>	
						</tr>
				<%
					    for (GroupRequest i : requests) {
				%>
								<tr>
								<td><%=i.getOwner()%></td>
								<td><%=i.getGroupName()%></td>
								<form action="/database" method="post">
									<input type="hidden" name="mode" value="acceptRequest">
									<input type="hidden" name="key" value="<%=i.getKey()%>">
									<input type="hidden" name="user" value="<%=user.getNickname()%>">
									<input type="hidden" name="group" value="<%=i.getGroup()%>">
									<td><input type="submit" value="Accept"></td>
								</form>
								<form action="/database" method="post">
									<input type="hidden" name="mode" value="rejectRequest">
									<input type="hidden" name="key" value="<%=i.getKey()%>">
									<td><input type="submit" value="Reject"></td>
								</form>
								</tr>
		<%	
						}
		%>
						</table>
		<%
					}
		            q = pm.newQuery(MapUserToGroup.class);
		            q.setFilter("user == u || user == u2");
		            q.declareParameters("String u, String u2");
				    List<MapUserToGroup> myGroupMap = (List<MapUserToGroup>) q.execute(user.getNickname().toLowerCase(),user.getEmail().toLowerCase());
				    
				    if( !(myGroupMap.isEmpty()) )
				    {
				    	List<ShoppingGroup> myGroups = new ArrayList<ShoppingGroup>();
				    	for( MapUserToGroup mug : myGroupMap )
				    	{
				    		ShoppingGroup sg = pm.getObjectById(ShoppingGroup.class, KeyFactory.stringToKey(mug.getGroup()));
				    		if(sg != null)
				    			myGroups.add(sg);
				    	}
				    if (!(myGroups.isEmpty()))
				    {
				%>
		    			<table cellpadding="2" cellspacing="2" width="100%">
				<%
					    for (ShoppingGroup i : myGroups) {
				%>
								<tr></tr><tr><td><%=i.getName()%><td></tr>
								<table cellpadding="2" cellspacing="2" width="100%">
								<tr>
								<td bgcolor="#cccccc">Group Members</td>
								<td bgcolor="#cccccc">Membership</td>
								</tr>
								<%
									q = pm.newQuery(MapUserToGroup.class);
						            q.setFilter("group == currentGroup");
						            q.declareParameters("String currentGroup");
								    List<MapUserToGroup> groupUsers = (List<MapUserToGroup>) q.execute(i.getKey());
								    if (!(groupUsers.isEmpty()))
								    {
								    	for(MapUserToGroup j: groupUsers)
								    	{
								    	%>
								    		<tr><td><%=j.getUser()%></td>
								    		<% 
								    		if (j.getUser().toLowerCase().equals(user.getNickname().toLowerCase()) || j.getUser().toLowerCase().equals(user.getEmail().toLowerCase()))
								    		{
								    		%>
								    			<td> SELF </td></tr>
								    		<%
								    		} else {
								    			if(i.getCreator().equals(user.getEmail().toLowerCase()) || i.getCreator().equals(user.getNickname().toLowerCase()))
												{								    		
										    		%>
										  			<form action="/database" method="post">
														<input type="hidden" name="mode" value="kickuser">
														<input type="hidden" name="entry" value="<%=j.getKey()%>">											
														<td><input type="submit" value="Kick"></td>
													</form>
													</tr>
									    			<%
								    			}
								    			else
								    			{
								    				if(i.getCreator().equals(j.getUser()))
								    				{
							    						%>
							    						<td> OWNER </td></tr>
							    						<%
								    				}
							    					else
							    					{
							    						%>
							    						<td> MEMBER </td></tr>
							    						<%
							    					}
								    			}
								    		} //end else
								    	} //end for group users 
								    }//end if empty
								    if(i.getCreator().equals(user.getEmail().toLowerCase()) || i.getCreator().equals(user.getNickname().toLowerCase()))
								    {
								    	%>
						    		<form action="/database" method="post">
										<input type="hidden" name="mode" value="inviteuser">
										<input type="hidden" name="group" value="<%=i.getKey()%>">
										<input type="hidden" name="owner" value="<%=user.getNickname()%>">
										<input type="hidden" name="groupname" value="<%=i.getName()%>">
										<td><input type="text" name="user" size="30" maxlength="20"></td>									
										<td><input type="submit" value="Invite"></td>
									</form>
								</table>
								
										<%
									}
								    q = pm.newQuery(ShoppingItem.class);
									q.setFilter("global_list == false && group == mygroup");
									q.declareParameters("String mygroup");
								    List<ShoppingItem> inventory = (List<ShoppingItem>) q.execute(i.getKey());
								    q = pm.newQuery(ShoppingItem.class);
								    q.setFilter("global_list == true");
								    List<ShoppingItem> globalList = (List<ShoppingItem>) q.execute();
								    if (inventory.isEmpty()) {
								%>
								<p>There are no items in the db</p>
										    <table cellpadding="2" cellspacing="2" width="100%">
											<tr>
											<td bgcolor="#cccccc">Item Name</td>
											<td bgcolor="#cccccc">Item Type</td>
											<td bgcolor="#cccccc">Added by</td>
											<td bgcolor="#cccccc">Group</td>
											</tr>
											<tr>
								<%
								    } else {
								%>
								    		<table cellpadding="2" cellspacing="2" width="100%">
											<tr>
											<td bgcolor="#cccccc">Item Name</td>
											<td bgcolor="#cccccc">Item Type</td>
											<td bgcolor="#cccccc">Added by</td>
											<td bgcolor="#cccccc">Group</td>
											</tr>
											<tr>
								<%	
								    for (ShoppingItem item : inventory) {
								%>
											<td><%=item.getName()%></td>
											<td><%=item.getType()%></td>
											<td><%=item.getOwner()%></td>
											<td><%=item.getGroup()%></td>
											<form action="/database" method="post">
												<input type="hidden" name="mode" value="removeFromList">
												<input type="hidden" name="key" value="<%=item.getKey()%>">
												<td><input type="submit" value="delete"></td>
											</form>
											</tr>
								<%	
								        }
								    }
								%>
													
								  <form action="/database" method="post">
											<tr>
											<td><input type="hidden" name="mode" value="addListItem">
											<input type="hidden" name="group" value="<%=i.getKey()%>">
											<input type="hidden" name="owner" value="<%=user.getNickname()%>">
											<select name="name">
											<%
												for( ShoppingItem listItem : globalList )
												{
											%>
												<option value="<%=listItem.getName()+"~"+listItem.getType()%>"><%=listItem.getName()%>/<%=listItem.getType()%></option>
											<%
												}
											%>
											<td><input type="submit" value="submit" /></td>
											</tr>
								  </form>
								  </table>
		<%	
						} //for shoppingroup i
		%>
						</table>
		<%
					}
					}//groupmap is empty
					pm.close();
		    } else {
		%>
				<p>Hello! Please
				<a href="<%= userService.createLoginURL(request.getRequestURI()) %>"> Sign in</a> to use site</p>
		<%
		    }
		%>
  </body>
</html>